package com.skywolf.chem.common.sysconfig.utils;

/**
 * Created by pei_pei on 2020/10/27.
 */

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

public class ChineseNumberUtils {
    private static final String[] BEFORE_SCALE = new String[]{"万", "仟", "佰", "拾", "亿", "仟", "佰", "拾", "万", "仟", "佰", "拾", ""};
    private static final String[] AFTER_SCALE = new String[]{"角", "分"};
    private static final String DEFAULT_PATH_SEPARATOR = ".";
    private static final Map<String, String> NUMBER_MAPPING = new HashMap();

    public ChineseNumberUtils() {
    }

    public static String getChineseNumber(String number) {
        return getChineseNumber((String)number, (String)null, (String)null);
    }

    public static String getChineseNumber(String number, String unit, String postfix) {
        if(number != null && !"".equals(number)) {
            String[] numbers = number.split("\\.");
            if(numbers.length > 2) {
                new NumberFormatException("数字格式错误!");
            }

            int length = numbers[0].length();
            int isZero = 0;
            StringBuffer result = new StringBuffer();

            int j;
            for(j = 0; j < length; ++j) {
                String digit = String.valueOf(numbers[0].charAt(j));
                boolean allZero = true;

                for(int hasValue = j; hasValue < length; ++hasValue) {
                    if(numbers[0].charAt(hasValue) != 48) {
                        allZero = false;
                        break;
                    }
                }

                if(allZero) {
                    boolean var12 = false;

                    for(int z = j; z >= 0; --z) {
                        if(numbers[0].charAt(z) != 48 && length - z <= 7 && length - z >= 5) {
                            var12 = true;
                            break;
                        }
                    }

                    if(length - j > 4 && length <= 8 || var12 && length - j > 4) {
                        result.append(BEFORE_SCALE[8]);
                    }

                    if(length - j >= 9) {
                        result.append(BEFORE_SCALE[4]);
                    }
                    break;
                }

                if(length < 9 && length - j == 5) {
                    if(!"0".equals(digit) && isZero > 0) {
                        result.append((String)NUMBER_MAPPING.get("0"));
                    }

                    if("0".equals(digit)) {
                        result.append(BEFORE_SCALE[8]);
                        if(isZero > 0) {
                            result.append((String)NUMBER_MAPPING.get("0"));
                        }
                        continue;
                    }
                }

                if("0".equals(digit) && length > 9 && length - j == 9) {
                    result.append(BEFORE_SCALE[4]);
                } else {
                    if(isZero < 1 || !"0".equals(digit)) {
                        if("0".equals(digit)) {
                            if(length - j != 6 && length - j != 7) {
                                result.append((String)NUMBER_MAPPING.get(digit));
                            }
                        } else {
                            result.append((String)NUMBER_MAPPING.get(digit));
                        }

                        if(!"0".equals(digit)) {
                            result.append(BEFORE_SCALE[BEFORE_SCALE.length - length + j]);
                        }
                    }

                    if("0".equals(digit)) {
                        ++isZero;
                    } else {
                        isZero = 0;
                    }
                }
            }

            result.append((CharSequence)(unit == null?"圆":result.append(unit)));
            if(numbers.length == 1) {
                result.append((CharSequence)(postfix == null?"整":result.append(postfix)));
                return result.toString();
            } else {
                length = numbers[1].length();

                for(j = 0; j < length && j <= 2; ++j) {
                    if(numbers[1].charAt(j) != 48) {
                        result.append((String)NUMBER_MAPPING.get(String.valueOf(numbers[1].charAt(j))));
                        result.append(AFTER_SCALE[j]);
                    }
                }

                result.append((CharSequence)(postfix == null?"整":result.append(postfix)));
                return result.toString();
            }
        } else {
            return null;
        }
    }

    public static String getChineseNumber(int number) {
        return getChineseNumber(new Integer(number));
    }

    public static String getChineseNumber(int number, String unit, String postfix) {
        return getChineseNumber(new Integer(number), unit, postfix);
    }

    public static String getChineseNumber(Long number) {
        return getChineseNumber((String)number.toString(), (String)null, (String)null);
    }

    public static String getChineseNumber(Integer number) {
        return getChineseNumber((String)number.toString(), (String)null, (String)null);
    }

    public static String getChineseNumber(Integer number, String unit, String postfix) {
        return getChineseNumber(number.toString(), unit, postfix);
    }

    public static String getChineseNumber(Long number, String unit, String postfix) {
        return getChineseNumber(number.toString(), unit, postfix);
    }

    public static String getChineseNumber(long number) {
        return getChineseNumber(new Long(number));
    }

    public static String getChineseNumber(long number, String unit, String postfix) {
        return getChineseNumber(new Long(number), unit, postfix);
    }

    public static String getChineseNumber(double number, String unit, String postfix) {
        DecimalFormat f = (DecimalFormat)DecimalFormat.getInstance();
        f.applyLocalizedPattern("#.##");
        return getChineseNumber(f.format(number), unit, postfix);
    }

    public static String getChineseNumber(double number) {
        return getChineseNumber(number, (String)null, (String)null);
    }

    public static String getChineseNumber(Double number) {
        return getChineseNumber(number.doubleValue());
    }

    public static String getChineseNumber(Double number, String unit, String postfix) {
        return getChineseNumber(number.doubleValue(), unit, postfix);
    }

    public static void main(String[] args) {
        System.out.println(getChineseNumber(1994));
        System.out.println(getChineseNumber(1994.1115D));
        System.out.println(getChineseNumber(19941115));
    }

    static {
        NUMBER_MAPPING.put("0", "零");
        NUMBER_MAPPING.put("1", "壹");
        NUMBER_MAPPING.put("2", "贰");
        NUMBER_MAPPING.put("3", "叁");
        NUMBER_MAPPING.put("4", "肆");
        NUMBER_MAPPING.put("5", "伍");
        NUMBER_MAPPING.put("6", "陆");
        NUMBER_MAPPING.put("7", "柒");
        NUMBER_MAPPING.put("8", "捌");
        NUMBER_MAPPING.put("9", "玖");
    }
}

